<?php

namespace App\Http\Controllers\index;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Carbon\Carbon;

class index extends controller
{
	//首页页面显示
	public function index()
	{
		$c = [
            'hot' => '热门',
            'often' => '常用'
        ];

	    $a = [
	        'hot' => null,
            'often' => '常用'
        ];

      //轮播图
	    $cha = DB::table('index')->where('cycle',1)->get();
	  //热门
	    $re = DB::table('index')->where($c)->get();
	  //常用服务
        $ri = DB::table('index')->where($a)->get();
      //服务介绍内容
        $fu = DB::table('index_introduce')->get();
      //服务
        $wu = DB::table('index')->where('often','服务')->limit(2)->get();

	    //赋值渲染
	    return view('index.index',['lun'=>$cha,'re'=>$re,'ri'=>$ri,'fu'=>$fu,'wu'=>$wu]);
    }


	public function cleaning_index(Request $request)
	{
//	    ->orderBy('id', 'desc')->limit(7)
      //时间段表连接阿姨id与需要上班的日期表id
      $rows = DB::table('index_time')->join('index_aunt', 'index_aunt.id', '=', 'index_time.pid')
              ->join('index_date','index_date.id', '=', 'index_time.rid')->get();
      $rows = json_decode($rows,true);
//      $rows = array_reverse($rows);

        //  用户预约
                $shijian = [];
                $dd = [];
                foreach($rows as $v){
                    $v = (array)$v;
                    $shijian[] = $v['date'];
                }
                $shijian = array_unique($shijian);
                foreach($shijian as $a){
                    foreach($rows as $k => $c){
                        if($c['date'] == $a){
                            $sj = [];
                            $aysj = [];
                            $sj[]  = json_decode($c['time']); //循环出日期的时间
                            $aysj[] = json_decode($c['aytime']); //循环出阿姨的0-1
                            $k = array_combine($sj[0],$aysj[0]);
                            $dd[$a][$c['name']] = $k; //将日期的时间对应阿姨的空闲时间0是空闲1是没时间
                        }
                    }
                }



            //查看预约满的时间
                    $date = [];
                    foreach ($dd as $kw=>$vw){
                        foreach($vw as $l => $j){
                            foreach($j as $n => $m){
                                if(!isset($date[$kw][$n])){
                                    $date[$kw][$n] = $m;
                                }else if($m == 0 || $date[$kw][$n] == 0){
                                    $date[$kw][$n] = 0;
                                }else if($date[$kw][$n] == 1 && $m = 1){
                                    $date[$kw][$n] = 1;
                                }
                            }
                        }
                    }

        $time=[
        ];

        $tsj=[];
        foreach ($date as $k1=>$v1){
                $tsj[]=$v1;
        }

        foreach ($dd as $kw => $vw) {                                   //$kw是日期 循环日期 一个循环就是一天的数据
           foreach ($date[$kw] as $ks => $vs) {                         //$ks是时间，一个循环就是一个时间段
              foreach ($dd[$kw] as $ka=>$va){                           //$ka是阿姨id 一个循环就是一个阿姨
                if($dd[$kw][$ka][$ks]==0){                               //一天当中代表某个时间段的阿姨可预约
                  $date[$kw][$ks].=','.$ka;                              //拼凑某个时间段有时间工作的阿姨id
                  if(!isset($time[$kw][$ka]))$time[$kw][$ka]=0;
                  $time[$kw][$ka]++;                                     //计算某个阿姨空闲时间的次数
                }
              }
           }
        }



        $id = $request->Input('id')??'';
        if($id == ''){
            $cha='';
            return view('index.cleaning_index',['cha'=>$cha,'date' => $date,'time' => $time,'tsj'=>$tsj]);
        }
        $cha = DB::table('index_address')->where('id',$id)->first();
        $kan = DB::table('index_city')->where('id',$cha->cid)->first();
        if($cha != ''){
            if($kan == ''){
                $cha->tel = '你的地址不在服务区';
                $kan->money = 2;
                return view('index.cleaning_index',['cha'=>$cha,'kan'=>$kan,'date' => $date,'time' => $time,'tsj'=>$tsj]);
            }
	       return view('index.cleaning_index',['cha'=>$cha,'kan'=>$kan,'date' => $date,'time' => $time,'tsj'=>$tsj]);

       }
    }


	//地板打蜡页面显示
    public function floor(Request $request)
    {
        $name = session('name');
        if($name == null){
            return view('index.login.login');
        }
//        ->orderBy('id', 'desc')->limit(7)
        //时间段表连接阿姨id与需要上班的日期表id
//        $damm =  DB::table('index_date')->orderBy('id', 'desc')->limit(7)->get();

        $rows = DB::table('index_time')->join('index_aunt', 'index_aunt.id', '=', 'index_time.pid')
                 ->join('index_date','index_date.id', '=', 'index_time.rid')->get();
        $rows = json_decode($rows,true);


        //  用户预约
        $shijian = [];
        $dd = [];
        foreach($rows as $v){
            $v = (array)$v;
            $shijian[] = $v['date'];
        }
        $shijian = array_unique($shijian);
        foreach($shijian as $a){
            foreach($rows as $k => $c){
                if($c['date'] == $a){
                    $sj = [];
                    $aysj = [];
                    $sj[]  = json_decode($c['time']); //循环出日期的时间
                    $aysj[] = json_decode($c['aytime']); //循环出阿姨的0-1
                    $k = array_combine($sj[0],$aysj[0]);
                    $dd[$a][$c['name']] = $k; //将日期的时间对应阿姨的空闲时间0是空闲1是没时间
                }
            }
        }



        //查看预约满的时间
        $date = [];
        foreach ($dd as $kw=>$vw){
            foreach($vw as $l => $j){
                foreach($j as $n => $m){
                    if(!isset($date[$kw][$n])){
                        $date[$kw][$n] = $m;
                    }else if($m == 0 || $date[$kw][$n] == 0){
                        $date[$kw][$n] = 0;
                    }else if($date[$kw][$n] == 1 && $m = 1){
                        $date[$kw][$n] = 1;
                    }
                }
            }
        }

        $time=[
        ];

        $tsj=[];
        foreach ($date as $k1=>$v1){
            $tsj[]=$v1;
        }

        foreach ($dd as $kw => $vw) {                                   //$kw是日期 循环日期 一个循环就是一天的数据
            foreach ($date[$kw] as $ks => $vs) {                         //$ks是时间，一个循环就是一个时间段
                foreach ($dd[$kw] as $ka=>$va){                           //$ka是阿姨id 一个循环就是一个阿姨
                    if($dd[$kw][$ka][$ks]==0){                               //一天当中代表某个时间段的阿姨可预约
                        $date[$kw][$ks].=','.$ka;                              //拼凑某个时间段有时间工作的阿姨id
                        if(!isset($time[$kw][$ka]))$time[$kw][$ka]=0;
                        $time[$kw][$ka]++;                                     //计算某个阿姨空闲时间的次数
                    }
                }
            }
        }


        $fuwu = $request->get('fuwu') ?? '';
        $id = $request->Input('id') ?? '';
        if ($id == '') {
            $cha = '';
            return view('index.floor', ['cha' => $cha,'fuwu'=>$fuwu,'date' => $date,'time' => $time,'tsj'=>$tsj]);
        }

        $cha = DB::table('index_address')->where('id', $id)->first();
        $kan = DB::table('index_city')->where('id', $cha->cid)->first();
        if($kan == ''){
            $kan->money= '你的地址不在服务区';
        }
            if ($cha != '') {
            return view('index.floor', ['cha' => $cha, 'kan' => $kan,'fuwu'=>$fuwu,'date' => $date,'time' => $time,'tsj'=>$tsj]);
        }
    }

	//会员充值页面显示
	public function recharge()
	{
	    $res = Db::table('index_user')->where('name',session('name'))->first();
	    $row = Db::table('index_package')->get();
//	    dd($row);
	    return view('index.recharge',['res' => $res,'row' => $row]);
	}

	//添加金额
	public function recharge_add(Request $request)
    {
        //获取ajax传输过来的数据
        $data = $request->input('data');
        //把获取到的数据转换成数组
        $a=json_decode($data);
//        dd($a);
        //查询用户
        $res = Db::table('index_user')->where('name',$a->user)->first();
        //获取当前时间
        $time = date('Y-m-d H:i:s',time());
        $htime=date("Y-m-d H:i:s",strtotime("+7 day"));
        //判断用户选择的是其他还是固定金额
        if(!isset($a->dc)){
                //把金额相加
                $je = $res->money+$a->cz;
                //充值次数
                $cs = $res->number+1;
                //判断是否是第一次充值并且充值金额大于100
                if($res->number > 0) {
                    //dd(1111);
                    $row = Db::table('index_user')->where('name', $a->user)->update(['money' => $je, 'number' => $cs]);
                    if (!$row) {
                        return ['res' => 0];
                    }
                    $rew = ['pid' => $res->id, 'detail' => '充值', 'time' => $time, 'change' => '+', 'money' => $a->cz, 'balance' => '1'];
                    $ros = Db::table('index_money')->insert($rew);
                    return ['res' => 1];
                }
                //  dd(123);
                $jei = $res->money + $a->cz + 100;
                //修改金额和充值次数
                $row = Db::table('index_user')->where('name', $a->user)->update(['money' => $jei, 'number' => $cs]);
                if (!$row) {
                    return ['res' => 0];
                }
                $rew = ['pid' => $res->id, 'detail' => '充值', 'time' => $time, 'change' => '+', 'money' => $a->cz+100, 'balance' => '1'];
                $ros = Db::table('index_money')->insert($rew);
                return ['res' => 1];
            }
            //把金额相加
            $je = $res->money+$a->dc;
            //充值次数
            $cs = $res->number+1;
            //判断是否是第一次充值并且充值金额大于100
            if($res->number > 0 && $a->dc >= 100){
                //dd(1111);
                //修改金额和充值次数
                $row = Db::table('index_user')->where('name',$a->user)->update(['money'=>$je,'number'=>$cs]);
                if(!$row){
                    return ['res'=>0];
                }
                //准备添加在余额表的数组
                $rew = ['pid'=>$res->id,'detail'=>'充值','time'=>$time,'change'=>'+','money'=>$a->dc,'balance'=>'1'];
                //把数组添加到余额表中
                $ros = Db::table('index_money')->insert($rew);
                $qian=$res->money+$a->dc;
                if(!isset($a->yhj1)){
                    $qita=DB::table('index_user')->where('id',$res->id)->update(['money'=>$qian]);
                    return ['res'=>1];
                }
                $yhjcx=Db::table('index_yhj')->where('yhj',$a->yhj1)->first();
                if($yhjcx){
                    $acc = $yhjcx->yhj_pieces+1;
                    $arr=['u_id'=>$res->id,'jine'=>$a->jine,'yhj'=>$a->yhj1,'date_start'=>$time,'date_the'=>$htime,'where'=>$a->yhj2,'title'=>$a->yhj3,'yhj_pieces'=>$acc];
                }
                $arr1=['u_id'=>$res->id,'jine'=>$a->jine,'yhj'=>$a->yhj1,'date_start'=>$time,'date_the'=>$htime,'where'=>$a->yhj2,'title'=>$a->yhj3,];
                //dd($res->money>1000);
                if(!$res->money>1000){
                    $rows=Db::table('index_yhj')->insert($arr1);
                }
                $rows = Db::table('index_yhj')->where(['u_id'=>$res->id,'yhj'=>$a->yhj1])->update($arr);
                return ['res'=>1];
            }
            //dd(12312);
            $jei = $res->money+$a->dc+100;
            //dd($jei);
            //修改金额和充值次数
            $row = Db::table('index_user')->where('name',$a->user)->update(['money'=>$jei,'number'=>$cs]);
            if(!$row){
                return ['res'=>0];
            }
            //准备添加在余额表的数组
            $rew = ['pid'=>$res->id,'detail'=>'充值','time'=>$time,'change'=>'+','money'=>$a->dc+100,'balance'=>'1'];
            //把数组添加到余额表中
            $ros = Db::table('index_money')->insert($rew);
            $qian=$res->money+$a->dc;
            if(isset($a->yhj1)) {
                $yhjcx = Db::table('index_yhj')->where('yhj', $a->yhj1)->first();
                if (!$res->money>1000) {
                    $arr1 = ['u_id' => $res->id,'jine'=>$a->jine,'yhj' => $a->yhj1, 'date_start' => $time, 'date_the' => $htime, 'where' => $a->yhj2, 'title' => $a->yhj3];
                    $rows = Db::table('index_yhj')->insert($arr1);
                }
                $acc = $yhjcx->yhj_pieces + 1;
                $arr = ['u_id' => $res->id,'jine'=>$a->jine,'yhj' => $a->yhj1, 'date_start' => $time, 'date_the' => $htime, 'where' => $a->yhj2, 'title' => $a->yhj3, 'yhj_pieces' => $acc];
                $rows = Db::table('index_yhj')->where(['u_id' => $res->id, 'yhj' => $a->yhj1])->update($arr);
                return ['res'=>1];
            }
            return ['res'=>1];
    }

	//开通会员页面显示
	public function dredge_member()
	{
		return view('index.dredge_member');
	}

	//服务地址页面显示
    public function address(Request $request)
    {
        //接收
        $fuwu = $request->get('fuwu');
        $name = session('name');
        $jie = $request->Input()??null;
        $di = $request->Input('ii')??null;
        //判断是否登录
        if($name == null){
            return view('index.login.login');
        }else if($jie == null || $di !=  null){
            //判断是哪一位用户
            $yong = DB::table('index_user')->where('name',$name)->first();
            //查出这位用户的所有地址
            $cha = DB::table('index_address')->where('pid',$yong->address)->get();
//            dd($di);
            return view('index.address',['cha'=>$cha,'li'=>$di,'fuwu'=>$fuwu]);

        }
        //判断是哪一位用户
        $yong = DB::table('index_user')->where('name',$name)->first();
        //赋值
        $add=[
            'pid' => $yong->address,
            'contact' => $jie['lian'],
            'community' => $jie['community'],
            'tel' => $jie['tel'],
            'address' => $jie['qu'],
            'cid' => $jie['cid']
        ];
        $tian = DB::table('index_address')->insert($add);
        if($tian){
            return 0;
        }
    }

    //执行服务地址删除
    public function delete_community(Request $request){
        $id = $request->get('id');
        $shan = DB::table('index_address')->where('id',$id)->delete();
        if($shan){
            return 0;
        }
    }
	//服务地址添加页面显示
	public function address_add(Request $request)
	{
        $name =session('name');
        if($name == null){
            return view('index.login.login');
        }
	    $id = $request->get('id')??'';
	    if($id != null){
	        $cha = DB::table('index_city')->where('id',$id)->first();
            return view('index.address_add',['cha'=>$cha]);
        }
        $cha='';
        return view('index.address_add',['cha'=>$cha]);
    }

	//服务地址搜索页面显示
	public function address_search(Request $request)
	{
        $name =session('name');
        if($name == null){
            return view('index.login.login');
        }
	    //接收搜索传过来的值
	    $community = $request->get('community');
        //判断用户是否搜索
        if($community!=null){
            //判断搜索的小区是否在服务区
            $cha = DB::table('index_city')->where('community',"like","%".$community."%")->get()->toArray();
            if(!empty($cha)){
                return $cha;
            }
            return 0;
        }
        return view('index.address_search');
    }

    //确认订单
    public function affirm_order()
    {
        return view('index.affirm_order');
    }

    //确认订单
    public function WeiMeng()
    {
        return view('index.WeiMeng');
    }
	//订单页面
	public function myorder(Request $request)
	{
	    $name =session('name');
        if($name == null){
            return view('index.login.login');
        }
        $ding = $request->post()??null;
        if($ding != null) {
            //查看是哪位用户点的阿姨
            $yh = DB::table('index_user')->where('name', $name)->first();
            //时间段表连接阿姨id与需要上班的日期表id
            $rows = DB::table('index_time')->join('index_aunt', 'index_aunt.id', '=', 'index_time.pid')
                ->join('index_date', 'index_date.id', '=', 'index_time.rid')->get();
            $rows = json_decode($rows, true);

            $riqi = $ding['ding'][7];
            $xiaoshi = $ding['ding'][8];

            //  用户预约
            $shijian = [];
            $dd = [];
            foreach ($rows as $v) {
                $v = (array)$v;
                $shijian[] = $v['date'];
            }
            $shijian = array_unique($shijian);
            foreach ($shijian as $a) {
                foreach ($rows as $k => $c) {
                    if ($c['date'] == $a) {
                        $sj = [];
                        $aysj = [];
                        $sj[] = json_decode($c['time']); //循环出日期的时间
                        $aysj[] = json_decode($c['aytime']); //循环出阿姨的0-1
                        $k = array_combine($sj[0], $aysj[0]);
                        $dd[$a][$c['name']] = $k; //将日期的时间对应阿姨的空闲时间0是空闲1是没时间
                    }
                }
            }


            //查看预约满的时间
            $date = [];
            foreach ($dd as $kw => $vw) {
                foreach ($vw as $l => $j) {
                    foreach ($j as $n => $m) {
                        if (!isset($date[$kw][$n])) {
                            $date[$kw][$n] = $m;
                        } else if ($m == 0 || $date[$kw][$n] == 0) {
                            $date[$kw][$n] = 0;
                        } else if ($date[$kw][$n] == 1 && $m = 1) {
                            $date[$kw][$n] = 1;
                        }
                    }
                }
            }

            $b = 0;$aaaa=0;$a=[];
            $aya = null;
            //分配阿姨接单
            foreach ($dd as $kw => $vs) {
                foreach ($date as $ks => $ko) {
                    $b = 0;
                    if ($ks == $riqi) {
                        foreach ($ko as $ss => $kn) {
                            $b++;
                            if ($ss == $xiaoshi && $kn == 0) {  //判断用户选择的日期下面的时间是否有空闲时间
                                if ($kw == $ks && $kw == $riqi) {                 //判断阿姨的那一天是否有时间
                                    foreach ($vs as $axs => $ak) {
                                            foreach ($ak as $af=>$am) {
                                                if ($am == $kn && $af == $ss) {        //进入了客户选择的时间段有时间的阿姨里面
                                                    $a = [
                                                        $riqi => [$axs => [$ss => $b]]
                                                    ];
                                                    $aya = $axs;$aaaa++;
                                                    $adad = [
                                                        'userid' => $yh->id,                         //用户id
                                                        'publishedtime' => $ding['ding'][1],     //发布的时间
                                                        'money' => $ding['ding'][2],                     //需要付款的金额
                                                        'address_pid' => $ding['ding'][6],         //用户的地址
                                                        'state' => 0,                           //订单的状态
                                                        'content' => $ding['ding'][3],                 //订单类型
                                                        'single' => date(time()) . $yh->id,                 //订单号
                                                        'area' => $ding['ding'][0],                       //面积
                                                        'aid' => $axs,                                 //阿姨姓名
                                                        'time' => '半个小时',                           //工作了几个小时
                                                        'contact' => $ding['ding'][4],                 //联系人
                                                        'tel' => $ding['ding'][5]                          //联系方式
                                                    ];
                                                    $add = DB::table('index_order')->insert($adad);
                                                    if(!$add){
                                                        return 13;
                                                    }
                                                    break;
                                                }
                                            }
                                    }
                                    break;
                                }
                                break;
                            }
                        }
                        break;
                    }
                }
            }

            if ($aaaa !=0) {
                $gai = [
                    'index_date.date' => $riqi,
                    'index_aunt.name' => $aya,
                ];
                $rowss = DB::table('index_time')->join('index_aunt', 'index_aunt.id', '=', 'index_time.pid')
                    ->join('index_date', 'index_date.id', '=', 'index_time.rid')->where($gai)->select('index_time.id','index_time.aytime')->first();
                $aytime = json_decode($rowss->aytime);
                $aytime[$b-1] = 1;
                $aytimee=[
                    'aytime'=>json_encode($aytime),
                    'id'=>$rowss->id
                ];
                  $rowsss = DB::table('index_time')->where('id',$rowss->id)->update($aytimee);
                if($rowsss){
                    return 11;
                }

            }
            return 12;
        }
        $yhm = DB::table('index_user')->where('name', session('name'))->first();
		$res = Db::table('index_order')->where('userid',$yhm->id)->get();
//        dd($res);
        return view('order.myorder',['res' => $res]);
	}

    //订单详情
    public function order(Request $request)
    {
        $id = $request->input('id');
//        dd($id);
        $a = $request->input('a');
        $times = date('Y-m-d H:i:s',time());
        $uid = Db::table('index_user')->where('name',session('name'))->first();
        $zje = $uid->money-$a;
        $res = Db::table('index_order')->where('id',$id)->first();
        $time = strtotime($res->publishedtime."+30 minutes");
        $datetime = date('Y-m-d H:i:s', $time);
        $dqsj = date("Y-m-d H:i:s",time());
//        dd($datetime <= $dqsj);
        if($res->state == 1){
            if($datetime <= $dqsj){
                $row = Db::table('index_order')->where('id',$id)->update(['state' => 4]);
                return view('order.order',['res' => $res]);
            }else{
                return view('order.order_underway',['res' => $res]);
            }
        }else{
            if($datetime <= $dqsj){
                $row = Db::table('index_order')->where('id',$id)->update(['state' => 3]);
                return view('order.order',['res' => $res]);
            }else{
                $row = Db::table('index_order')->where('id',$id)->update(['state' => 1]);
                $fkje = Db::table('index_user')->where('id',$uid->id)->update(['money' => $zje]);
                $rews = ['pid' => $uid->id, 'detail' => '消费', 'time' => $times, 'change' => '-', 'money' => $a, 'balance' => '1'];
                $ros = Db::table('index_money')->insert($rews);
                return view('order.order_underway',['res' => $res]);
            }
        }
    }

    //取消订单
    public function order_out(Request $request){
        $id = $request->input('name');
        $money = $request->input('money');
        $time = date('Y-m-d H:i:s',time());
        $ros = Db::table('index_user')->where('name',session('name'))->first();
        $zje = $ros->money+$money;
        $row = Db::table('index_order')->where('id',$id)->update(['state' => 3]);
        if($row){
            $rows = Db::table('index_user')->where('id',$ros->id)->update(['money' => $zje]);
            $rew = ['pid' => $ros->id, 'detail' => '退款', 'time' => $time, 'change' => '+', 'money' => $money, 'balance' => '1'];
//            dd($rew);
            $ross = Db::table('index_money')->insert($rew);
            return 1;
        }else{
            return 0;
        }
    }

    //订单评价
    public function evaluate()
    {
        return view('order.evaluate');
    }

    //我的
    public function index_center()
    {
        if(session('name')!=''){
            $rows=DB::table('index_user')->where('name',session('name'))->value('img');
        }else{
            $rows="/blue/images/touxiang2.png";
        }
        return view('mine.index_center',['rows'=>$rows]);
    }

    //我的钱包
    public function my_money()
    {
        $user_id = Db::table('index_user')->where('name',session('name'))->get();
        return view('mine.my_money',['user_id'=>$user_id]);
    }

    //钱包
    public function money()
    {
        //查询一个用户的数据
        $res = Db::table('index_user')->where('name',session('name'))->first();
        //查询全部用户的数据
        $user_id = Db::table('index_user')->where('name',session('name'))->get();
        //查出余额表的pid等于用户表id的数据
        $money = Db::table('index_money')->where('pid',$res->id)->get();
//      dump($money);
        return view('mine.money',['user_id'=>$user_id,'money'=>$money]);
    }

    //常见问题
    public function problem_mould()
    {
        return view('mine.problem_mould');
    }

    //优惠劵
    public function coupon()
    {
        $u_id=Db::table('index_user')->where('name',session('name'))->first();
        $res = Db::table('index_yhj')->where('u_id',$u_id->id)->get();

        return view('mine.coupon',['res' => $res]);
    }

    //会员中心
    public function member_center()
    {
        $res = Db::table('index_user')->where('name',session('name'))->first();
        return view('mine.member_center',['res'=>$res]);
    }


    //邀请分享
    public function share()
    {
        return view('mine.share');
    }

     //消息中心
    public function news()
    {
        $res = Db::table('index_news')->get();
        return view('mine.news',['res' => $res]);
    }
    //修改头像
    public function upimg(Request $request){
        $file = $request->file('img');
        $filename=$file->getClientOriginalName();
        $path=$file->move("blue/images/",$filename);
        $pathname=$path->getPath().'/'.$path->getFilename();
        $rows=DB::table('index_user')->where('name',session('name'))->update(['img'=>$pathname]);
        return 1;
    }
}


